package com.example.delicioushunter.view.activity;

import java.util.ArrayList;

import com.example.delicioushunter.R;
import com.example.delicioushunter.R.layout;
import com.example.delicioushunter.R.menu;
import com.example.delicioushunter.control.databasecontrol.MaterialControl;
import com.example.delicioushunter.model.database.DHDatabaseHelper;
import com.example.delicioushunter.model.object.Book;
import com.example.delicioushunter.model.object.Material;
import com.example.delicioushunter.view.adapter.MaterialAdapter;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup.MarginLayoutParams;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Gallery;
import android.widget.Toast;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.GridView;

public class StorageActivity extends Activity {

	private GridView galleryMaterial01, galleryMaterial02, galleryMaterial03,
			galleryMaterial04, galleryMaterial05;
	private Context mContext;
	private ArrayList<Material> materials;
	private MaterialAdapter adapter;
	private DHDatabaseHelper helper;
	private SQLiteDatabase rdb;
	private SQLiteDatabase wdb;
	private MaterialControl control;
	private ProgressDialog pd;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext = this;
		setContentView(R.layout.activity_storage);

		galleryMaterial01 = (GridView) findViewById(R.id.galleryMaterial01);
		galleryMaterial02 = (GridView) findViewById(R.id.galleryMaterial02);
		galleryMaterial03 = (GridView) findViewById(R.id.galleryMaterial03);
		galleryMaterial04 = (GridView) findViewById(R.id.galleryMaterial04);
		galleryMaterial05 = (GridView) findViewById(R.id.galleryMaterial05);

		materials = new ArrayList<Material>();
		adapter = new MaterialAdapter(mContext, materials);

		galleryMaterial01.setAdapter(adapter);
		galleryMaterial01.setOnItemLongClickListener(mOnItemLongClick);
		galleryMaterial02.setAdapter(adapter);
		galleryMaterial02.setOnItemLongClickListener(mOnItemLongClick);
		galleryMaterial03.setAdapter(adapter);
		galleryMaterial03.setOnItemLongClickListener(mOnItemLongClick);
		galleryMaterial04.setAdapter(adapter);
		galleryMaterial04.setOnItemLongClickListener(mOnItemLongClick);
		galleryMaterial05.setAdapter(adapter);
		galleryMaterial05.setOnItemLongClickListener(mOnItemLongClick);
	}

	private OnItemLongClickListener mOnItemLongClick = new OnItemLongClickListener() {

		@Override
		public boolean onItemLongClick(AdapterView<?> parent, View v,
				int position, long id) {
			if (materials.get(position).get_id() != 0) {
				control = new MaterialControl();
				wdb = helper.getWritableDatabase();
				control.deleteMaterial(materials.get(position).get_id(), wdb);
				Toast.makeText(mContext, "Delete Successfully",
						Toast.LENGTH_SHORT).show();
				materials.remove(position);
				adapter.notifyDataSetChanged();
			}
			return false;
		}
	};

	private void loadMaterials() {
		LoadMaterialsTask task = new LoadMaterialsTask();
		task.execute();
	}

	private class LoadMaterialsTask extends
			AsyncTask<Void, Void, ArrayList<Material>> {

		@Override
		protected ArrayList<Material> doInBackground(Void... params) {
			try {
				helper = new DHDatabaseHelper(mContext);
				rdb = helper.getReadableDatabase();
				control = new MaterialControl();
				return control.getAllMaterials(rdb);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return null;
		}

		@Override
		protected void onPostExecute(ArrayList<Material> result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			materials.clear();
			for (Material m : result) {
				addMaterial(m);
			}

			result.clear();
			pd.dismiss();
		}

		@Override
		protected void onPreExecute() {
			pd = new ProgressDialog(mContext, R.style.MyDialogTheme);
			pd.setMessage("Loading...");
			pd.show();
		}

	}

	private void addMaterial(Material m) {
		materials.add(0, m);
		adapter.notifyDataSetChanged();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.storage, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.action_add_material:
			pd.dismiss();
			Intent intent = new Intent(StorageActivity.this,
					AddMaterialActivity.class);
			startActivity(intent);
			break;

		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onResume() {
		super.onResume();
		loadMaterials();
	}

}
